﻿Imports System.Data.OleDb
Imports System.IO
Imports System.Data.OleDb.OleDbDataReader
Public Class ArticleAdmin
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("userName") = User.Identity.Name
        If User.IsInRole("admin") Then
            articleWriter.Visible = True
        End If
        If User.IsInRole("user") Or User.Identity.IsAuthenticated = False Then
            Response.Redirect("Default.aspx")
        End If

        If Not IsPostBack Then
            ErrorMessage.Visible = False
        End If
        randomQuote()
    End Sub
    Sub randomQuote()
        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        conn.Open()
        Dim cmd As New OleDbCommand("SELECT TOP 6 * FROM [FamousQuotes] ORDER BY rnd(INT(NOW*id)-NOW*id)", conn)
        quoteCascade.DataSource = cmd.ExecuteReader()
        quoteCascade.DataBind()
        conn.Close()
    End Sub
Protected Sub HTMLEditorExtender_ImageUploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs)  
        Dim filePath As String = "~/img/ar/" & Convert.ToString(e.FileName)
        TextBox1_HtmlEditorExtender.AjaxFileUpload.SaveAs(MapPath(filePath))
        e.PostedUrl = Page.ResolveUrl(filePath)
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      
        If String.IsNullOrEmpty(TextBox1.Text) Then
        ErrorMessage.Visible = True
        ErrorMessage.Text = "Your submission is blank. Please write your article first"
        Else
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
            Dim SqlString As String = "Insert into UserArticles(Title,Category,UserName,DateOfPost,HTMLPost) Values (@f1,@f2,@f3,@f4,@f5)"
            Dim HTMLEncode As String = Server.HtmlEncode(TextBox1.Text)
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", ArticleTitle.Text)
            cmd.Parameters.AddWithValue("@f2", CategoryDropDown.SelectedValue)
            cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f4", DateTime.Now.Date)
            cmd.Parameters.AddWithValue("@f5", HTMLEncode)
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            TextBox1.Text = Nothing
            ArticleTitle.Text = Nothing
            CategoryDropDown.ClearSelection()
        End If
End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBox1.Text = Nothing
    End Sub
End Class